import os
import sys

script_path = os.path.abspath(__file__)
script_dir = os.path.dirname(script_path)
sys.path.append(script_dir)
from baseClass.other import *
from baseClass.plant import *
from .bullet import peas_Bullet
from role.obj.scene import *


# 豌豆射手
class peashooter_plant(Plant):
    """
    豌豆射手

    初始化参数：
        生命：300
        攻击力：50
        射速：1

    """

    def __init__(self, x, y, sprite_group: pygame.sprite.Group):
        super().__init__(
            BVolume=Life(300),
            KSpeed=KSpeed(1),
            Damage=Damage(50),
            Type=TERRESTRIAL_PLANTS,
        )
        self.gif_frame = gif_frame("../../PVZ素材/植物/豌豆射手.gif")
        self.image = self.gif_frame.action()
        self.rect = self.image.get_rect()
        self.rect.left = x
        self.rect.bottom = y
        self.rect.width = self.rect.width * 0.6
        self.last_attack_time = pygame.time.get_ticks() - 2500
        self.last_update_time = pygame.time.get_ticks()
        self.sprite_group = sprite_group

    def attack(self):
        """攻击方法"""
        if pygame.time.get_ticks() - self.last_attack_time > 2500:
            self.last_attack_time = pygame.time.get_ticks()
            self.sprite_group.add(
                peas_Bullet(x=self.rect.x + self.rect.width, y=self.rect.y)
            )

    def update(self, *args, **kwargs):
        """更新方法"""
        if pygame.time.get_ticks() - self.last_update_time > 50:
            self.last_update_time = pygame.time.get_ticks()
            self.image = self.gif_frame.action()
            self.attack()
